package com.gxa.weixing.pc.controller;

import com.gxa.weixing.core.common.ExceptionCode;
import com.gxa.weixing.core.common.JsonResult;
import com.gxa.weixing.core.exception.BusinessException;
import com.gxa.weixing.core.pojo.City;
import com.gxa.weixing.core.pojo.GoldRun;
import com.gxa.weixing.core.pojo.TemplateRule;
import com.gxa.weixing.core.util.StringUtil;
import com.gxa.weixing.pc.service.CityService;
import com.gxa.weixing.pc.service.TemplateRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author ChenJ
 * @date 2022/01/10 10:23
 **/
@RestController
@RequestMapping("/freight")
@Api(value = "freightController",tags = "运费管理接口")
public class FreightController {

    @Resource
    private CityService cityService;

    @Resource
    private TemplateRuleService templateRuleService;

    @GetMapping("/getCity")
    @ApiOperation("获取下级城市的接口")
    @ApiImplicitParam(name = "id", value = "父级编号",dataType = "Long", paramType = "query")
    public Map<String ,Object> getCity(Long id) throws BusinessException {
        List<City> city = cityService.getCity(id);
        if (city.size()==0){
            throw new BusinessException(ExceptionCode.WRONG_SERVER);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.setData(city);
        return jsonResult.getDataMap();
    }

    @GetMapping("/getCityById")
    @ApiOperation("根据编号获取城市的接口")
    @ApiImplicitParam(name = "id", value = "城市编号",dataType = "Long",required = true, paramType = "query")
    public Map<String ,Object> getCityById(@RequestParam Long id) throws BusinessException {
        City city = cityService.selecyId(id);
        if (city==null){
            throw new BusinessException(ExceptionCode.WRONG_SERVER);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.setData(city);
        return jsonResult.getDataMap();
    }

    @PostMapping("/insert")
    @ApiOperation("新增运费模板接口")
    @ApiImplicitParam(name = "templateRule", value = "模板对象",dataType = "Long", paramType = "body",dataTypeClass = TemplateRule.class)
    public Map<String ,Object> insert(@RequestBody TemplateRule templateRule) throws BusinessException {
        if (templateRule==null){
            throw new BusinessException(ExceptionCode.NO_DATA);
        }
        TemplateRule insert = templateRuleService.insert(templateRule);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/update")
    @ApiOperation("修改运费模板接口")
    @ApiImplicitParam(name = "templateRule", value = "模板对象",dataType = "TemplateRule", paramType = "body",dataTypeClass = TemplateRule.class)
    public Map<String ,Object> update(@RequestBody TemplateRule templateRule) throws BusinessException {
        if (templateRule==null){
            throw new BusinessException(ExceptionCode.NO_DATA);
        }
        Integer integer = templateRuleService.updateById(templateRule);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @GetMapping("/getList")
    @ApiOperation("获取全部运费模板列表")
    public Map<String ,Object> getList() throws BusinessException {
        List<TemplateRule> templateRules = templateRuleService.selectAll();
        if (templateRules.size()==0){
            throw new BusinessException(ExceptionCode.NO_DATA);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.setData(templateRules);
        return jsonResult.getDataMap();
    }


}
